import requests
import ddddocr
from bs4 import BeautifulSoup
from lxml import etree


ocr = ddddocr.DdddOcr(show_ad=False)

# 1. 获取隐藏域

url_login_get = "https://www.gushiwen.cn/user/login.aspx"
response_login_get = requests.get(url_login_get)
soup = BeautifulSoup(response_login_get.content, "lxml")
v1 = soup.find(id="__VIEWSTATE")['value']
v2 = soup.find(id="__VIEWSTATEGENERATOR")['value']

# 2. 获取验证码
url_img = "https://www.gushiwen.cn/RandCode.ashx"
response_img = requests.get(url_img)
with open("./static/古诗文网/验证码.jpg", "wb") as f:
    f.write(response_img.content)
r = ocr.classification(response_img.content)
img_cookies = response_img.cookies

# 3. 模拟登录, 获取cookies
data = {
    "__VIEWSTATE": v1,
    "__VIEWSTATEGENERATOR": v2,
    "email": "15703822596",
    "pwd": "wert123",
    "code": f"{r}"
}

url_login_post = "https://www.gushiwen.cn/user/login.aspx"
response_login_post = requests.post(url_login_post, data=data, cookies=img_cookies)
print(response_login_post.cookies)


# 4. 获取我的收藏

favorite_url = "https://www.gushiwen.cn/user/collect.aspx?type=m&id=7557102&sort=t"

response = requests.get(favorite_url, cookies=response_login_post.cookies)
print(response.text)









